package listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import view.MainFrame;
import view.RotateGlassPane;
import controller.LayerController;
import controller.ToolBarController;

public class RotateGlassPaneListener implements ActionListener, ChangeListener,MouseListener {

	@Override
	public void stateChanged(ChangeEvent arg0) {
		RotateGlassPane glassPane = (RotateGlassPane) MainFrame.getInstance()
				.getGlassPane();
		glassPane.setAngle(glassPane.getSliderRotation().getValue());
		glassPane.repaint();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		JButton butSrc = (JButton) arg0.getSource();
		if (butSrc.getName().equals("cancel")) {
			ToolBarController.glassPaneOff();
		}

		if (butSrc.getName().equals("confirm")) {
			LayerController
					.rotateLayer(MainFrame.getInstance().getDrawingPane()
							.getSelectedLayer(), ((RotateGlassPane) MainFrame
							.getInstance().getGlassPane()).getAngle());
			ToolBarController.glassPaneOff();
			MainFrame.getInstance().repaint();
		}
		if (butSrc.getName().equals("rotate_left")) {
			JSlider slider = ((RotateGlassPane) MainFrame.getInstance()
					.getGlassPane()).getSliderRotation();

			int current = slider.getValue();
			if (current < -90) {
				slider.setValue(-180);
			} else {
				slider.setValue(slider.getValue() - 90);
			}
		}
		if (butSrc.getName().equals("rotate_right")) {
			JSlider slider = ((RotateGlassPane) MainFrame.getInstance()
					.getGlassPane()).getSliderRotation();

			int current = slider.getValue();
			if (current > 90) {
				slider.setValue(180);
			} else {
				slider.setValue(slider.getValue() + 90);
			}
		}
	}
	
	//----- NOT USED-----

	@Override
	public void mouseClicked(MouseEvent arg0) {
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
	}
}
